package com.lazy.utils.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.lazy.utils.exception.ProgramException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * FastJson封装工具类
 *
 * @author fangqi
 * @date 2012-3-8
 */
public final class FastJsonUtil {
    private FastJsonUtil() {

    }

    /**
     * 将对象转换为json
     *
     */
    public static String obj2json(Object value) {
        try {
            if(value instanceof String){
                return (String) value;
            }
            return JSON.toJSONString(value);
        } catch (Exception e) {
            try {
                return obj2json3(value);
            } catch (Exception e1) {
                throw new ProgramException("FastJson序列化异常：" + value, e1);
            }
        }
    }

    public static String obj2jsonPretty(Object value) {
        try {
            return JSON.toJSONString(value, SerializerFeature.PrettyFormat);
        } catch (Exception e) {
            try {
                return obj2json2(value);
            } catch (Exception e1) {
                throw new ProgramException("FastJson序列化异常：" + value, e1);
            }
        }
    }

    public static String obj2jsonDateFormat(Object value) {
        try {
            return JSON.toJSONStringWithDateFormat(value, "yyyy-MM-dd HH:mm:ss",
                    SerializerFeature.WriteDateUseDateFormat,SerializerFeature.DisableCircularReferenceDetect);
        } catch (Exception e) {
            try {
                return obj2json2(value);
            } catch (Exception e1) {
                throw new ProgramException("FastJson序列化异常：" + value, e1);
            }
        }
    }

    public static String obj2jsonDateFormat(Object value, String format) {
        try {
//			return JSON.toJSONString(value, SerializerFeature.PrettyFormat);
            return JSON.toJSONStringWithDateFormat(value, format,
                    SerializerFeature.WriteDateUseDateFormat);
        } catch (Exception e) {
            try {
                return obj2json2(value);
            } catch (Exception e1) {
                throw new ProgramException("FastJson序列化异常：" + value, e1);
            }
        }
    }

    @SuppressWarnings("deprecation")
    public static String obj2json2(Object value) {
        try {
            return JSON.toJSONString(value, SerializerFeature.WriteTabAsSpecial, SerializerFeature.WriteSlashAsSpecial);
        } catch (Exception e) {
            throw new ProgramException(e);
        }
    }

    @SuppressWarnings("deprecation")
    public static String obj2json3(Object value) {
        try {
            return JSON.toJSONString(value, SerializerFeature.WriteTabAsSpecial, SerializerFeature.WriteSlashAsSpecial,
                    SerializerFeature.BrowserCompatible);
        } catch (Exception e) {
            throw new ProgramException(e);
        }
    }

    /**
     * 将json转换为对象
     *
     * @param <T>   转换的类型
     * @param json  json数据
     * @param clazz 被转化对象的class
     * @return
     */
    public static <T> T json2obj(String json, Class<T> clazz) {
        try {
            return JSON.parseObject(json, clazz);
        } catch (Exception e) {
            throw new ProgramException("json字符串不能解析。" + json, e);
        }
    }

    /**
     * 将json转换为对象数组
     *
     * @param <T>   转换的类型
     * @param json  json数据
     * @param clazz 被转化对象的class
     * @return
     */
    public static <T> List<T> json2List(String json, Class<T> clazz) {
        try {
            return JSON.parseArray(json, clazz);
        } catch (Exception e) {
            throw new ProgramException("json字符串不能解析。" + json, e);
        }
    }

    /**
     * jsonStr to Map
     */
    public static Map<String, String> parseJSON2Map(String jsonStr) {

        // 参数di
        Map<String, String> map = new HashMap<String, String>();
        JSONObject json = null;

        // 最外层解析
        try {
            json = JSONObject.parseObject(jsonStr);
            for (String k : json.keySet()) {
                Object v = json.get(k);
                if (null != v) {
                    map.put(k.toString(), v.toString());
                }
            }
        } catch (Exception e) {
            throw new ProgramException(e);
        }
        return map;
    }

    /**
     * jsonStr to Map
     *
     * @param jsonStr jsonStr
     * @return
     */
    public static Map<String, Object> parseJSON2Map2(String jsonStr) {

        // 参数di
        Map<String, Object> map = new HashMap<String, Object>();
        JSONObject json = null;

        // 最外层解析
        try {
            json = JSONObject.parseObject(jsonStr);
            for (String k : json.keySet()) {
                Object v = json.get(k);
                if (null != v) {
                    map.put(k.toString(), v.toString());
                }
            }
        } catch (Exception e) {
            throw new ProgramException(e);
        }
        return map;
    }
}
